//app.js
App({
  onLaunch: function () {

    let that = this;
    wx.getSystemInfo({
      success: res => {

        console.log('手机信息res111' + res.brand)
        let brand = res.brand.toUpperCase();

        if (brand.search('HUAWEI') != -1) {

          that.globalData.isHuaWei = true

        }

        this.globalData.navHeight = res.statusBarHeight + 46;
      }
    })
    
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              console.log(res)
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    });

    this.getPosition()

  },
  getPosition() {
    let that = this;

    wx.authorize({ scope: "scope.userLocation" })

    wx.getLocation({
      type: 'wgs84',
      success: res => {
        var latitude = res.latitude;
        var longitude = res.longitude;
        // wx.setStorageSync('latitude', latitude) //纬度
        // wx.setStorageSync('longitude', longitude) //经度
        var qqMapApi = this.globalData.qqMapApi + "?location=" + latitude + ',' +
          longitude + "&key=" + this.globalData.qqMapKey + "&get_poi=1";

        console.log("请求地址为", qqMapApi)
        wx.request({
          url: qqMapApi,
          data: {},
          method: 'GET',
          success: (res) => {
            console.log(res)
            if (res.statusCode == 200 && res.data.status == 0) {
              this.globalData.country = res.data.result.address_component.nation;
              this.globalData.province = res.data.result.address_component.province;
              this.globalData.city = res.data.result.address_component.city;
              this.globalData.county = res.data.result.address_component.district;
              this.globalData.street = res.data.result.address_component.street;
              this.globalData.lat = res.data.result.location.lat;
              this.globalData.lng = res.data.result.location.lng;
              console.log("获取到城市", this.globalData.city)
            }
          }
        })
      },
      fail() {
        //that.fn_fail();
        console.log("获取城市失败")
      }
    })

  },
  /*请求地址*/
  requestUrl: "https://mp.fot-tech.com",
  /*发送请求 */
  request(param = {}, successFn, failFn) {
    wx.request({
      "url": this.requestUrl + param.url,
      "method": param.method || "POST",
      "data": param.data || {},
      "success": (res) => {       
          successFn && successFn(res.data);        
      },
      "fail": () => {
        wx.hideLoading();
        if (failFn) {
          failFn();
        } else {
          this.showToast("网络错误！请重试！");
        }
      }
    });
  },
  globalData: {
    userInfo: null,
    loading: false,
    canuse: true,
    isHuaWei: false,
    the_host:"https://mp.fot-tech.com",
    qqMapApi:"https://apis.map.qq.com/ws/geocoder/v1/",
    qqMapKey:"WEGBZ-VQ5KQ-4IS5Y-G3GUU-TYD5Q-LMFBA",
    access_token:'',
    carNumber:0,
    carList:[],
    ip:'127.0.0.1',
    
  },
  
})